<?php

namespace App\Business\BLL;

use App\Jobs\SendReservationMsg;

class ReservationBLL
{
    public function startReservation($playA, $playB = null, $game)
    {
        $vaildA = $playA->validateReservationA($game->game_datetime);
        if ($vaildA['status']) {
            $game->play_a = $playA->id;
        } else {
            return $vaildA;
        }
        if ($playB) {
            $vaildB = $playB->validateReservationB();
            if ($vaildB['status']) {
                $game->play_b = $playB->id;
            } else {
                return $vaildB;
            }

            if ($game->play_a == $game->play_b) {
                return ['status' => false, 'msg' => '不能约战自己'];
            }
        }

        $game->city = $playA->city;
        $game->save();
        //发送通知
        dispatch((new SendReservationMsg($game)));

        return ['status' => true, 'id' => $game->id, 'msg' => '比赛创建成功'];

    }
}
